from datetime import datetime
from enum import Enum
from typing import List, Union

from fastapi import Query
from pydantic import BaseModel, Field, RootModel
from typing_extensions import Literal

from forward.api.admin.schemas.system import EmptyStrToNone
from forward.common.enums import WXOauthScopeEnum
from forward.utils.tools import ToolsUtil


class SettingWebsiteInSchema(BaseModel):
    """
    保存网站信息参数
    """

    name: str = Field(default="")  # 网站名称
    logo: str = Field(default="")  # 网站图标
    favicon: str = Field(default="")  # 网站LOGO
    backdrop: str = Field(default="")  # 登录页广告图
    shop_name: str = Field(default="", alias="shopName")  # 商城名称
    shop_logo: str = Field(default="", alias="shopLogo")  # 商城Logo


class SettingCopyrightItemSchema(BaseModel):
    """
    备案通用参数
    """

    name: str  # 名称
    link: str  # 链接


class SettingCopyrightInSchema(RootModel[List[SettingCopyrightItemSchema]]):
    """
    保存备案信息参数
    """

    pass


class CommonProtocolSchema(BaseModel):
    """
    政策通用参数
    """

    name: str  # 名称
    content: str  # 内容


class SettingProtocolInSchema(BaseModel):
    """
    保存政策信息参数
    """

    service: CommonProtocolSchema  # 服务协议
    privacy: CommonProtocolSchema  # 隐私协议


class SettingStorageDetailInSchema(BaseModel):
    """存储设置详情入参"""

    alias: str


class SettingStorageEditInSchema(BaseModel):
    """存储设置配置入参"""

    alias: str
    status: int
    bucket: Union[str, None]
    secretKey: Union[str, None]
    accessKey: Union[str, None]
    domain: Union[str, None]
    region: Union[str, None]


class SettingStorageChangeInSchema(BaseModel):
    """切换存储入参"""

    alias: str
    status: int


class SettingStorageOutSchema(BaseModel):
    """存储设置列表返回"""

    alias: str
    name: str
    status: int
    describe: str = ""


class SettingStorageDetailOutSchema(SettingStorageOutSchema):
    """存储设置详情返回"""

    bucket: str = ""
    secretKey: str = ""
    accessKey: str = ""
    domain: str = ""


class SettingDictTypeListInSchema(BaseModel):
    """
    字典类型-列表 入参
    """

    dictName: Union[str, None, EmptyStrToNone]
    dictType: Union[str, None, EmptyStrToNone]
    dictStatus: Union[int, None, EmptyStrToNone]


class SettingDictTypeAddInSchema(BaseModel):
    """
    字典类型-新增 入参
    """

    dict_name: str = Field(alias="dictName")
    dict_type: str = Field(alias="dictType")
    dict_remark: Union[str, None] = Field(alias="dictRemark")
    dict_status: int = Field(alias="dictStatus")


class SettingDictTypeEditInSchema(SettingDictTypeAddInSchema):
    """
    字典类型-编辑 入参
    """

    id: int


class SettingDictTypeDetailInSchema(BaseModel):
    """
    字典类型-删除 入参
    """

    id: int


class SettingDictTypeDeleteInSchema(BaseModel):
    """
    字典类型-删除 入参
    """

    ids: List[int]


class SettingDictTypeOutSchema(BaseModel):
    """
    字典类型 返回
    """

    id: int
    dict_name: str = Field()
    dict_type: str = Field()
    dict_remark: str | None = Field(None)
    dict_status: int = Field()
    created_time: datetime | None = Field(None)
    updated_time: datetime | None = Field(None)
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SettingDictDataListInSchema(BaseModel):
    """
    字典数据-列表 入参
    """

    dict_type: str
    name: Union[str, None, EmptyStrToNone] = None
    value: Union[str, None, EmptyStrToNone] = None
    status: Union[int, None, EmptyStrToNone] = None
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SettingDictDataDetailInSchema(BaseModel):
    """
    字典数据-详情 入参
    """

    id: int


class SettingDictDataDeletelInSchema(BaseModel):
    """
    字典数据-删除 入参
    """

    ids: List[int]


class SettingDictDataAddInSchema(BaseModel):
    """
    字典数据-新增 入参
    """

    type_id: int = Field()
    name: str
    value: str
    remark: str
    sort: int
    status: int
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SettingDictDataEditInSchema(SettingDictDataAddInSchema):
    """
    字典数据-编辑 入参
    """

    id: int


class SettingDictDataOutSchema(BaseModel):
    """
    字典类型 返回
    """

    id: int
    type_id: int = Field()
    name: str
    value: str
    remark: str
    sort: int
    status: int
    created_time: datetime | None = Field(None)
    updated_time: datetime | None = Field(None)
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class HotSearchItemSchema(BaseModel):
    """
    热门搜索通用参数
    """

    name: str  # 关键词
    sort: int  # 排序号


class SettingHotSearchInSchema(BaseModel):
    """
    热门搜索参数
    """

    is_hot_search: Literal[0, 1] = Field(alias="isHotSearch")  # 是否开启搜索 0/1
    list: List[HotSearchItemSchema]


class SettingHotSearchOutSchema(BaseModel):
    """
    热门搜索返回
    """

    id: int
    name: str  # 关键词
    sort: int  # 排序号


class SettingLoginInSchema(BaseModel):
    """
    登录设置保存参数
    """

    login_way: str = Field(default="", alias="loginWay")  # 登录方式, 逗号隔开
    force_bind_mobile: int = Field(
        default=0, alias="forceBindMobile"
    )  # 强制绑定手机 0/1
    open_agreement: int = Field(default=0, alias="openAgreement")  # 是否开启协议 0/1
    open_other_auth: int = Field(default=0, alias="openOtherAuth")  # 第三方登录 0/1
    auto_login_auth: str = Field(
        default="", alias="autoLoginAuth"
    )  # 第三方自动登录 逗号隔开
    wx_oauth_scope: WXOauthScopeEnum = Field(
        default=WXOauthScopeEnum.BASE, alias="wxOauthScope"
    )


class SettingUserInSchema(BaseModel):
    """
    用户设置保存参数
    """

    default_avatar: str = Field(default="", alias="defaultAvatar")  # 默认头像


class SettingSmsDetailInSchema(BaseModel):
    """
    短信引擎详情参数
    """

    alias: str  # 别名


class SettingSmsSaveInSchema(BaseModel):
    """
    短信引擎保存参数
    """

    name: str  # 名称
    alias: str  # 别名
    status: Literal[0, 1]  # 状态
    sign: Union[str, None] = Field()
    appId: Union[str, None] = Field(alias="appId")
    appKey: Union[str, None] = Field(alias="appKey")
    secretId: Union[str, None] = Field(alias="secretId")
    secretKey: Union[str, None] = Field(alias="secretKey")


class SettingNoticeListInSchema(BaseModel):
    """
    通知设置列表参数
    """

    recipient: int = Query(ge=1, le=2)  # 类型: 1=用户, 2=平台


class SettingNoticeListOutSchema(BaseModel):
    """
    通知设置列表返回
    """

    id: int
    name: str  # 场景名称
    type: str  # 通知类型: [1=业务, 2=验证]
    system_status: int = Field(default=0)  # 系统的通知
    sms_status: int = Field(default=0)  # 短信的通知
    oa_status: int = Field(default=0)  # 公众号通知
    mnp_status: int = Field(default=0)  # 小程序通知
    created_time: datetime | None = Field(None)  # 创建时间
    updated_time: datetime | None = Field(None)  # 更新时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SettingNoticeDetailInSchema(BaseModel):
    """
    通知设置详情参数
    """

    id: int = Query(gt=0)  # 主键


class SettingNoticeSchema(BaseModel):
    """
    通知设置详情返回
    """

    id: int
    name: str  # 场景名称
    type: int  # 通知类型: [1=业务, 2=验证]
    remarks: str  # 场景描述
    system_notice: str  # 系统的通知
    sms_notice: str  # 短信的通知
    oa_notice: str  # 公众号通知
    mnp_notice: str  # 小程序通知
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class SettingNoticeDetailOutSchema(BaseModel):
    """
    通知设置详情返回
    """

    id: int
    name: str  # 场景名称
    type: str  # 通知类型: [1=业务, 2=验证]
    remarks: str  # 场景描述
    systemNotice: dict  # 系统的通知
    smsNotice: dict  # 短信的通知
    oaNotice: dict  # 公众号通知
    mnpNotice: dict  # 小程序通知


class SettingNoticeTplSchema(BaseModel):
    """
    通知设置项模板
    """

    tpl_name: Union[str, None] = Field(alias="tplName")
    tpl_keyword: Union[str, None] = Field(alias="tplKeyword")
    tpl_content: Union[str, None] = Field(alias="tplContent")


class SettingNoticeItemSchema(BaseModel):
    """
    通知设置项
    """

    status: Union[Literal[0, 1, "0", "1"], None]  # 状态
    name: Union[str, None]  # 名称
    remark: Union[str, None]
    first: Union[str, None]
    title: Union[str, None]
    content: Union[str, None]
    template_id: Union[str, None] = Field(alias="templateId")
    template_sn: Union[str, None] = Field(alias="templateSn")
    tpl: Union[List[SettingNoticeTplSchema], None]


class SettingNoticeSaveInSchema(BaseModel):
    """
    通知设置保存参数
    """

    id: int
    system_notice: SettingNoticeItemSchema = Field(alias="systemNotice")
    sms_notice: SettingNoticeItemSchema = Field(alias="smsNotice")
    oa_notice: SettingNoticeItemSchema = Field(alias="oaNotice")
    mnp_notice: SettingNoticeItemSchema = Field(alias="mnpNotice")
